<?php

namespace admin\controllers;

use app\helpers\MoneyHelper;
use app\models\ar\UserAr;
use admin\components\BaseAuthController;
use app\models\PayLog;
use app\models\UserIdentity;
use app\models\UserInvite;

class MemberController extends BaseAuthController {

    public $name = '用户管理';

    public function actionIndex() {
        $model = new UserAr();
        $model->with = ['profile', 'userLevel'];
        $wd = trim($this->get('wd'));

        $params = $this->getParam();
        $params['phone'] = $wd;
        $dataProvider = $model->search($params);
        return $this->render(compact('dataProvider'));
    }

    public function actionAllow($id) {
        $model = UserAr::findOne($id);
        if (!$model || !$model->isRealIng()) {
            return $this->error();
        }
        return $model->realAllow();
    }

    public function actionDeny($id) {
        $user = UserAr::findOne($id);
        if (!$user || !$user->isRealIng()) {
            return $this->error();
        }

        $model = $user->identity;
        $model->setScenario('deny');

        if (!$model->load($this->post())) {
            return $this->render(compact('model'));
        }

        return $user->realDeny($model);
    }

    public function actionView($id) {
        $where = ['uid' => $id, 'type' => [PayLog::TYPE_AD_AWARD, PayLog::TYPE_AD_REBATE]];

        $beg_time = strtotime(date('Y-m-d'));
        $end_time = $beg_time + 86400 - 1;
        $total_income = PayLog::find()->where($where)->andFilterWhere(['between', 'ctime', $beg_time, $end_time])->sum('amount');

        $start_time = strtotime(date('Y-m-01'));
        $end_time = strtotime('+1 month -1 day', $start_time) + 86400 - 1;
        $month_income = PayLog::find()->where($where)->andFilterWhere(['between', 'ctime', $beg_time, $end_time])->sum('amount');

        return $this->render([
            'model' => $this->findModel($id, UserAr::className()),
            'total_income' => MoneyHelper::moneyFormat($total_income),
            'month_income' => MoneyHelper::moneyFormat($month_income)
        ]);
    }

    public function actionInvites($uid, $level = 2) {
        $model = new UserInvite();
        $model->where = ['like', 'parents', "$uid%", FALSE];
        $model->with = ['profile'];
        $dataProvider = $model->search(['level' => $level]);

        $this->view->title = '下级列表';
        return $this->render(compact('dataProvider'));
    }

    public function actionIncome($uid) {
        $where = ['uid' => $uid, 'type' => [PayLog::TYPE_AD_AWARD, PayLog::TYPE_AD_REBATE]];

        $beg_time = strtotime(date('Y-m-d'));
        $end_time = $beg_time + 86400 - 1;
        $total_income = PayLog::find()->where($where)->andFilterWhere(['between', 'ctime', $beg_time, $end_time])->sum('amount');

        $start_time = strtotime(date('Y-m-01'));
        $end_time = strtotime('+1 month -1 day', $start_time) + 86400 - 1;
        $month_income = PayLog::find()->where($where)->andFilterWhere(['between', 'ctime', $beg_time, $end_time])->sum('amount');

        return $this->render(compact('total_income', 'month_income'));
    }

    public function actionRebates($uid) {
        $model = new PayLog();
        $model->where = ['uid' => $uid, 'type' => PayLog::TYPE_AD_REBATE];
        // $model->with = ['profile'];
        $dataProvider = $model->search();
        return $this->render(compact('dataProvider'));
    }
}
